<?php
/*********************/
/*                   */
/* http://www.ymd.cc */
/*********************/
/*    QQ: 11121426   */
/*                   */
/*********************/

include_once("shopObject.php");

class mdl_memberattr extends shopObject
{

    public $defaultCols = "attr_name,attr_tyname,attr_required,attr_search,attr_option,attr_show,attr_picture,attr_url,attr_order";
    public $idColumn = "attr_id";
    public $adminCtl = "member/memberattr";
    public $textColumn = "attr_name";
    public $defaultOrder = array
    (
        0 => "attr_order",
        1 => "desc"
    );
    public $tableName = "sdb_member_attr";

    public function mdl_memberattr()
    {
        parent::shopobject();
        $data = $this->db->select("select attr_id from sdb_member_attr where attr_group = 'defalut' ");
        if (count($data) == 0) {
            $this->import_defalutattr();
        }
    }

    public function import_defalutattr()
    {
        $attrarray = array(
            array(
                "attr_name" => __("地区"),
                "attr_type" => "area",
                "attr_required" => "false",
                "attr_search" => "false",
                "attr_option" => "",
                "attr_show" => "true",
                "attr_valtype" => "",
                "attr_tyname" => __("系统默认"),
                "attr_order" => "4",
                "attr_group" => "defalut"
            ),
            array(
                "attr_name" => __("联系地址"),
                "attr_type" => "addr",
                "attr_required" => "false",
                "attr_search" => "false",
                "attr_option" => "",
                "attr_show" => "true",
                "attr_valtype" => "",
                "attr_tyname" => __("系统默认"),
                "attr_order" => "5",
                "attr_group" => "defalut"
            ),
            array(
                "attr_name" => __("姓名"),
                "attr_type" => "name",
                "attr_required" => "false",
                "attr_search" => "false",
                "attr_option" => "",
                "attr_show" => "true",
                "attr_valtype" => "",
                "attr_tyname" => __("系统默认"),
                "attr_order" => "1",
                "attr_group" => "defalut"
            ),
            array(
                "attr_name" => __("手机号码"),
                "attr_type" => "mobile",
                "attr_required" => "false",
                "attr_search" => "false",
                "attr_option" => "",
                "attr_show" => "true",
                "attr_valtype" => "number",
                "attr_tyname" => __("系统默认"),
                "attr_order" => "7",
                "attr_group" => "defalut"
            ),
            array(
                "attr_name" => __("固定电话"),
                "attr_type" => "tel",
                "attr_required" => "false",
                "attr_search" => "false",
                "attr_option" => "",
                "attr_show" => "true",
                "attr_valtype" => "number",
                "attr_tyname" => __("系统默认"),
                "attr_order" => "8",
                "attr_group" => "defalut"
            ),
            array(
                "attr_name" => __("邮编"),
                "attr_type" => "zip",
                "attr_required" => "false",
                "attr_search" => "false",
                "attr_option" => "",
                "attr_show" => "true",
                "attr_valtype" => "number",
                "attr_tyname" => __("系统默认"),
                "attr_order" => "6",
                "attr_group" => "defalut"
            ),
            array(
                "attr_name" => __("性别"),
                "attr_type" => "sex",
                "attr_required" => "false",
                "attr_search" => "false",
                "attr_option" => "",
                "attr_show" => "true",
                "attr_valtype" => "",
                "attr_tyname" => __("系统默认"),
                "attr_order" => "2",
                "attr_group" => "defalut"
            ),
            array(
                "attr_name" => __("出生日期"),
                "attr_type" => "date",
                "attr_required" => "false",
                "attr_search" => "false",
                "attr_option" => "",
                "attr_show" => "true",
                "attr_valtype" => "",
                "attr_tyname" => __("系统默认"),
                "attr_order" => "3",
                "attr_group" => "defalut"
            ),
            array(
                "attr_name" => __("安全问题"),
                "attr_type" => "pw_question",
                "attr_required" => "false",
                "attr_search" => "false",
                "attr_option" => "",
                "attr_show" => "true",
                "attr_valtype" => "",
                "attr_tyname" => __("系统默认"),
                "attr_order" => "9",
                "attr_group" => "defalut"
            ),
            array(
                "attr_name" => __("回答"),
                "attr_type" => "pw_answer",
                "attr_required" => "false",
                "attr_search" => "false",
                "attr_option" => "",
                "attr_show" => "true",
                "attr_valtype" => "",
                "attr_tyname" => __("系统默认"),
                "attr_order" => "10",
                "attr_group" => "defalut"
            ),
            array("attr_name" => "QQ", "attr_type" => "text", "attr_required" => "false", "attr_search" => "true", "attr_option" => "", "attr_show" => "false", "attr_valtype" => "", "attr_tyname" => "QQ", "attr_order" => "11", "attr_group" => "contact"),
            array("attr_name" => "MSN", "attr_type" => "text", "attr_required" => "false", "attr_search" => "false", "attr_option" => "", "attr_show" => "false", "attr_valtype" => "email", "attr_tyname" => "MSN", "attr_order" => "12", "attr_group" => "contact"),
            array("attr_name" => "Skype", "attr_type" => "text", "attr_required" => "false", "attr_search" => "false", "attr_option" => "", "attr_show" => "false", "attr_valtype" => "alphaint", "attr_tyname" => "Skype", "attr_order" => "13", "attr_group" => "contact"),
            array(
                "attr_name" => __("旺旺"),
                "attr_type" => "text",
                "attr_required" => "false",
                "attr_search" => "false",
                "attr_option" => "",
                "attr_show" => "false",
                "attr_valtype" => "",
                "attr_tyname" => __("旺旺"),
                "attr_order" => "14",
                "attr_group" => "contact"
            )
        );
        foreach ($attrarray as $k => $v) {
            $this->save($v);
        }
    }

    public function setVisibility($attr_id, $status)
    {
        $sql = "update sdb_member_attr set attr_show = '" . ($status ? "true" : "false") . "' where attr_id = " . intval($attr_id);
        return $this->db->exec($sql);
    }

    public function save($data)
    {
        $aRs = $this->db->query("SELECT * FROM sdb_member_attr WHERE 1=1");
        $sql = $this->db->getInsertSql($aRs, $data);
        $result = $this->db->exec($sql);
        if ($result) {
            return $this->db->lastInsertId();
        } else {
            return "";
        }
    }

    public function Remove($attr_id)
    {
        return $this->db->exec("delete from sdb_member_attr where attr_id = '" . $attr_id . "'");
    }

    public function getFieldById($attr_id)
    {
        return $this->db->selectRow("SELECT * FROM sdb_member_attr WHERE attr_id=" . intval($attr_id));
    }

    public function updatememattr($data, $attr_id)
    {
        $member =& $this->system->loadModel("member/member");
        if ($data['attr_type'] == "select") {
            $memberdate = $member->getList("member_id", "", 0, -1);
            $i = 0;
            for (; $i < count($memberdate); ++$i) {
                $valuedate = $member->getattrvalue($memberdate[$i]['member_id'], $attr_id);
                if (1 < count($valuedate)) {
                    $k = 1;
                    for (; $k < count($valuedate); ++$k) {
                        $member->deleteAllMattrvalues($attr_id, $memberdate[$i]['member_id'], $valuedate[$k]['value']);
                    }
                }
            }
        }
        if ($data['attr_type'] == "select" || $data['attr_type'] == "checkbox") {
            $olddate = unserialize($data['attr_option']);
            $tmpdate = $this->getFieldById($attr_id);
            $newdate = unserialize($tmpdate['attr_option']);
            sort($olddate);
            sort($newdate);
            if ($olddate != $newdate) {
                $result = array_diff($newdate, $olddate);
                sort($result);
                $i = 0;
                for (; $i < count($result); ++$i) {
                    $member->deletememberidattrid($attr_id, $result[$i]);
                }
            }
        }
        $aRs = $this->db->query("SELECT * FROM sdb_member_attr WHERE attr_id=" . intval($attr_id));
        $sSql = $this->db->getUpdateSql($aRs, $data);
        return !$sSql || $this->db->query($sSql);
    }

    public function getAlloption($member_id)
    {
        return $this->db->select("SELECT * FROM sdb_member_mattrvalue AS ma, sdb_member_attr AS at WHERE ma.attr_id = at.attr_id and        ma.member_id = '" . $member_id . "' order by at.attr_order asc;");
    }

    public function updateorder($order, $attr_id)
    {
        $sql = "update sdb_member_attr set  attr_order = " . $this->db->quote($order) . " where attr_id = " . intval($attr_id);
        $this->db->exec($sql);
    }

    public function getMaxOrder()
    {
        $sql = "select max(attr_order) as attr_order from sdb_member_attr";
        return $this->db->select($sql);
    }

    public function getCustomOption()
    {
        $sql = "select * from sdb_member_attr where attr_group != 'defalut'";
        return $this->db->select($sql);
    }

    public function getCustomValueById($memid)
    {
        $sql = "SELECT * FROM sdb_member_attr WHERE attr_show = 'true' AND attr_group != 'defalut' ORDER BY attr_order ";
        return $this->db->select($sql);
    }

}

?>
